package com.itdct.onflow.generator.template.web

import com.itdct.onflow.generator.config.GeneratorConfig
import com.itdct.onflow.generator.model.ClassModel
import com.itdct.onflow.generator.model.CompareResultModel
import com.itdct.onflow.generator.model.FieldModel

/**
 * @author Zhouwx
 * @date 2025/8/25 18:17:07
 * @version 1.0
 * @description
 */
class VueTypesTemplate(classModel: ClassModel, generatorConfig: GeneratorConfig) : BaseVueTemplate(classModel, generatorConfig) {
    init {
        filterFieldList.add("del")
        filterFieldList.add("version")
        filterFieldList.add("deleteUser")
        filterFieldList.add("deleteTime")
    }

    override fun render(): String {
        return """
import { BasePageQo } from '@/base/api/BaseQo'

export type ${classModel.className}ListVo = {
  ${rangeStart("listVo")}
${voFields()}
  ${rangeEnd("listVo")}
}

export type ${classModel.className}DetailVo = {
  ${rangeStart("detailVo")}
${voFields()}
  ${rangeEnd("detailVo")}
}

export type ${classModel.className}SearchQo = BasePageQo & {
  ${rangeStart("pageQo")}
${qoFields()}
  ${rangeEnd("pageQo")}
}

export type ${classModel.className}EditQo = {
  ${rangeStart("editQo")}
${qoFields()}
  ${rangeEnd("editQo")}
}

        """.trimIndent()
    }

    private fun voFields(): String {
        return fieldsGen("") { i, fieldModel ->
            voFieldContent(fieldModel)
        }
    }

    private fun voFieldContent(fieldModel: FieldModel): String {
        return """
|  ${fieldModel.name}${nullable(fieldModel)}: ${fieldModel.vueType}
            """.trimMargin()
    }

    private fun qoFields(): String {
        return fieldsGen("") { i, fieldModel ->
            qoFieldContent(fieldModel)
        }
    }

    private fun qoFieldContent(fieldModel: FieldModel): String {
        return """
|  ${fieldModel.name}?: ${fieldModel.vueType}
            """.trimMargin()
    }

    private fun nullable(it: FieldModel): String {
        if (it.nullable) {
            return "?"
        } else {
            return ""
        }
    }

    override fun onModify(compareResultModel: CompareResultModel, content: String): String {
        var content = content
        val addFields = compareResultModel.addFields
        content = insertFieldListContent(content, addFields, "listVo", { index, fieldModel -> voFieldContent(fieldModel) })

        content = insertFieldListContent(content, addFields, "detailVo", { index, fieldModel -> voFieldContent(fieldModel) })

        content = insertFieldListContent(content, addFields, "pageQo", { index, fieldModel -> qoFieldContent(fieldModel) })

        content = insertFieldListContent(content, addFields, "editQo", { index, fieldModel -> qoFieldContent(fieldModel) })

        val delFields = compareResultModel.delFields
        content = deleteRangeFunctionCode(content, "listVo", delFields, { fieldModel -> voFieldContent(fieldModel) })

        content = deleteRangeFunctionCode(content, "detailVo", delFields, { fieldModel -> voFieldContent(fieldModel) })

        content = deleteRangeFunctionCode(content, "pageQo", delFields, { fieldModel -> voFieldContent(fieldModel) })

        content = deleteRangeFunctionCode(content, "editQo", delFields, { fieldModel -> voFieldContent(fieldModel) })

        val modifyPairs = compareResultModel.modifyPairs
        content = modifyReplace(content, "listVo", modifyPairs, { fieldModel -> voFieldContent(fieldModel) })

        content = modifyReplace(content, "detailVo", modifyPairs, { fieldModel -> voFieldContent(fieldModel) })

        content = modifyReplace(content, "pageQo", modifyPairs, { fieldModel -> voFieldContent(fieldModel) })

        content = modifyReplace(content, "editQo", modifyPairs, { fieldModel -> voFieldContent(fieldModel) })

        return content
    }

    override fun subModuleName(): String {
        return "api"
    }

    override fun restPath(): String {
        return classModel.className + "/types.ts"
    }
}